#include "SquareRender2.h"

namespace Sivin {
  SquareRender2::SquareRender2() {
    mShader = std::make_unique<Shader>("./../shader/square/vert.vs", "./../shader/square/frag.fs");
    mVertexData = {
        0.5f, 0.5f, 0.0f,
        -0.5f, 0.5f, 0.0f,
        -0.5, -0.5f, 0.0f,//第一个三角形

        0.5f, 0.5f, 0.0f,//第二个三角形
        -0.5f, -0.5f, 0.0f,
        0.5f, -0.5f, 0.0f};
  }

  SquareRender2::~SquareRender2() {
  }

  void SquareRender2::onGLInit() {
    mShader->init();

    glGenVertexArrays(1, &mVAO);
    GLuint vbo;
    glGenBuffers(1, &vbo);

    glBindVertexArray(mVAO);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * mVertexData.size(), mVertexData.data(), GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
    glEnableVertexAttribArray(0);

    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
  }

  void SquareRender2::onRender() {
    mShader->start();
    glBindVertexArray(mVAO);
    glDrawArrays(GL_TRIANGLES, 0, sizeof(mVertexData));
    mShader->end();
  }


}// namespace Sivin